//
//  CaseManageController.m
//  KMedicine
//
//  Created by 曹牧 on 16/7/5.
//  Copyright © 2016年 caomu. All rights reserved.
//

#import "CaseManageController.h"
#import "YYHeadView.h"
#import "CaseListCell.h"
#import "AddCaseButton.h"
#import "AddCaseController.h"
#import "AkuCaseManageModel.h"
#import "AKuCaseManageCell.h"


//#define caseIdentifier @"casecell"

// 宏定义, 直接根据文件名称, 拼接文件的绝对路径,
#define AkukFilePath(fileName) ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName])


@interface CaseManageController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *tv_case;
@property (nonatomic, strong)YYHeadView *headView;
@property (nonatomic, strong)AddCaseButton *btn_case;

<<<<<<< HEAD

@property (nonatomic, strong) NSArray *listArray1;
=======
@property (nonatomic, strong) NSArray *dataArray;
>>>>>>> b9c10ee0816bad4c2bd5c2dd383a671356b74fd5

@end

@implementation CaseManageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // 模拟 数据  =======================
    NSMutableArray *array = [NSMutableArray array];
    
    for (int i = 0; i < 20; i++) {
        
        AkuCaseManageModel *model = [[AkuCaseManageModel alloc] init];
        
        model.commitTime = @"2008-08-08 08:08:08";
        model.commonIllness = [NSString stringWithFormat:@"血液科--->%d", i];
        model.basisType = [NSString stringWithFormat:@"呼呼呼呼--->%d", i];
        model.describe = [NSString stringWithFormat:@"%d+收到sad fsad fasd 的的的ladhslfjhsdfkjljhl了空间 asdf s ndfb,+++++++%d", i , arc4random_uniform(9999999)];
        
        if (i%2==0) {
            
            model.iconImg = @"sssssss";
        }
        
        [array addObject:model];
        
    }
   
     [NSKeyedArchiver archiveRootObject:array toFile:AkukFilePath(@"caseManages.data")];

  
    
    // 初始化界面
    [self setupUI];
}

#pragma mark
#pragma mark 初始化界面
- (void)setupUI {
    
    //初始化底部 添加病例 按钮
    AddCaseButton *btn_case = [[AddCaseButton alloc] init];
    [btn_case setBackgroundImage:[UIImage imageNamed:@"nav"] forState:UIControlStateNormal];
    [btn_case setTitle:@"添加病例" forState:UIControlStateNormal];
    self.btn_case = btn_case;
    [btn_case addTarget:self action:@selector(addCase:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn_case];
    
    // 初始化 tableView
    UITableView *tbView = [[UITableView alloc] init];
    self.tv_case = tbView;
    self.tv_case.delegate = self;
    self.tv_case.dataSource = self;
    self.tv_case.bounces = NO;
    
    [self.tv_case registerClass:[AKuCaseManageCell class] forCellReuseIdentifier:@"cell"];
    [self.tv_case registerClass:[AKuCaseManageCell class] forCellReuseIdentifier:@"firstCell"];
    
    // 禁用自动偏移
    self.modalPresentationCapturesStatusBarAppearance = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    
    // MARK:tableView的背景颜色
    self.tv_case.tableFooterView = [UIView new];
    [self.view addSubview:self.tv_case];
    
    // 初始化headView
    self.headView = [[YYHeadView alloc] init];

    [self.view addSubview:self.headView];

    // MARK: 添加到 view后,设置约束
    [self setLayout];

}

#pragma mark
#pragma mark 添加病例按钮 监听方法
- (void)addCase:(UIButton *)sender{
    
    AddCaseController *addController = [[AddCaseController alloc] init];
    addController.view.backgroundColor = [UIColor whiteColor];
    addController.plistName = @"YYCase.plist";
    addController.navTitle = [sender currentTitle];
    [self.navigationController pushViewController:addController animated:YES];
    
}

#pragma mark
#pragma mark 设置约束
- (void)setLayout {
    
    
    
    
    // MARK:headView 的约束
    [self.headView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.offset(0);
        make.height.offset(160);
        
    }];
    
    [self.btn_case makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.offset(15);
        make.right.bottom.offset(-15);
        make.height.offset(40);
    }];
    
    // MARK:tableView的约束
    [self.tv_case makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.offset(0);
        make.top.equalTo(self.headView.bottom);
        make.bottom.equalTo(self.btn_case.top);
    }];
    
}


#pragma mark
#pragma mark tableView 代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
<<<<<<< HEAD
    return self.listArray1.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CaseListCell *cell = [tableView dequeueReusableCellWithIdentifier:caseIdentifier];
    if (cell == nil) {
        cell = [[CaseListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:caseIdentifier];

    }
    cell.model = self.listArray1[indexPath.row];
=======
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    
    NSString *cellIdextifier = @"cell";
    
    if (indexPath.row == 0) {
        
        cellIdextifier = @"firstCell";
    }
    
    AKuCaseManageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdextifier forIndexPath:indexPath];
    
    AkuCaseManageModel *model = self.dataArray[indexPath.row];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.modeCell = model;

>>>>>>> b9c10ee0816bad4c2bd5c2dd383a671356b74fd5
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

#pragma mark
#pragma mark 懒加载

<<<<<<< HEAD
- (NSArray *)listArray1{
    
    
    NSArray *temp = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"user.data")];
    if (temp == nil) {
        _listArray1 = [NSArray array];
    }else{
        _listArray1 = temp;
    }
    
    return _listArray1;
}


- (void)viewDidLayoutSubviews{
    [self.tv_case reloadData];
}
=======



#pragma mark - ljz 数组
- (NSArray *)dataArray {
    
    if (!_dataArray) {
        
        _dataArray = [NSKeyedUnarchiver unarchiveObjectWithFile:AkukFilePath(@"caseManages.data")];
    }
    return _dataArray;
}





>>>>>>> b9c10ee0816bad4c2bd5c2dd383a671356b74fd5
@end
